﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameName1
{
    class Projectile
    {

        public enum PROJ_TYPE { NONE = 0, WEP1, WEP2 };
        const float WEP1_DMG = 20.0f;
        const float WEP2_DMG = 25.0f;
        const float WEP3_DMG = 30.0f;


        Vector2 m_Pos;
        Vector2 m_Vel;
        Vector2 m_sPos;
        float m_radius;
        float m_dist;
        PROJ_TYPE m_type;
        float m_speed = 300.0f;

        GameObject source;
        Texture2D m_tex;

        public static Texture2D[] texture_array = new Texture2D[3];

        public static List<Projectile> proj = new List<Projectile>();

        //graphic
        //animation
        float m_damage;
        /// <summary>
        /// Initializes projectile
        /// </summary>
        /// <param name="i_Pos">Init position</param>
        /// <param name="i_vel">Init velocity</param>
        /// <param name="type">Type of projectile</param>
        /// <param name="src">Projectile Source</param>
        public Projectile(Vector2 i_Pos, Vector2 i_vel, PROJ_TYPE type, ref GameObject src)
        {
            m_Pos = i_Pos;
            m_sPos = i_Pos;
            m_Vel = i_vel*m_speed;
            m_type = type;

            source = src;
            m_dist = 100.0f;

            if(m_type == PROJ_TYPE.WEP1)
            {
                m_tex = texture_array[(int)PROJ_TYPE.WEP1];
            }
            else if (m_type == PROJ_TYPE.WEP2)
            {
                m_tex = texture_array[(int)PROJ_TYPE.WEP2];
            }
            //do things depending on type
        }

        public GameObject Source
        {
            get { return source;}
        }

        public Vector2 Velocity
        {
            get { return m_Vel;}
        }

        public Vector2 Position
        {
            get { return m_Pos; }
        }

        public float DistanceLeft
        {
            get { return m_dist; }
        }

        public float Radius
        {
            get { return m_radius; }
        }

        public PROJ_TYPE ProjType
        {
            get { return m_type;  }
        }

        public Texture2D Texture
        { //should be static
            get { return m_tex; }
            set { m_tex = value; }
        }
        protected void Update(double t)
        {
            if (m_dist < Math.Abs(Vector2.Distance(m_sPos, m_Pos)))
                Kill();

            m_Pos += m_Vel*(float)t;
            //float f = Math.Abs(Vector2.Distance(m_sPos, m_Pos));
            //m_dist -= Math.Abs(Vector2.Distance(m_sPos, m_Pos));
        }

        protected void Draw(ref SpriteBatch sbatch)
        {
            //DRAW CODE
            sbatch.Draw(m_tex, m_Pos, Color.White);
        }

        public void Kill()
        {
            Console.WriteLine("Proj died after travelling {0}", Math.Abs(Vector2.Distance(m_sPos, m_Pos)));
            proj.Remove(this);
        }

        public static void Add(Vector2 i_Pos, Vector2 i_vel, PROJ_TYPE type, ref GameObject src)
        {
            proj.Add(new Projectile( i_Pos,  i_vel, type, ref src));
        }

        public static void UpdateAll(double t)
        {
            if (proj.Count == 0)
                return;

            for (int i = 0 ; i < proj.Count ; i++ )
            {
                proj[i].Update(t);
            }
        }

        public static void DrawAll(ref SpriteBatch sbatch)
        {
            if (proj.Count == 0)
                return;

            for (int i = 0; i < proj.Count; i++)
            {
                proj[i].Draw(ref sbatch);
            }
        }

        public static void InitTexture()
        {

        }
    }
}
